﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.DAO;

namespace _XNA__RunningDeath.GUI
{
    class CGUI_Pause:CDiaLogBox
    {
        Texture2D _texBackground;
        Texture2D _texBtnBack;
        Texture2D _texBtnSetting;
        Texture2D _texBtnExitPause;
        Texture2D _textBtnSave;
        
        SpriteFont fontChu;

        bool isSusses = false;

        SaveGameData data;

        public CGUI_Pause(ContentManager content,GameTime gameTime,SpriteBatch spriteBatch):base(content,gameTime,spriteBatch)
        {
            this._texBackground = this._content.Load<Texture2D>(@"GUI/bgPause");
            this._texBtnBack = this._content.Load<Texture2D>(@"GUI/btnBack");
            this._texBtnExitPause = this._content.Load<Texture2D>(@"GUI/btnExitPause");
            this._texBtnSetting = this._content.Load<Texture2D>(@"GUI/btnSettingPause");
            this._textBtnSave = this._content.Load<Texture2D>(@"GUI/btnSave");
            this._texBG = this._content.Load<Texture2D>(@"GUI/guiBG");

            fontChu = content.Load<SpriteFont>("SpriteFont");

            this._dstoado = this._content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");
        }
        public override void Update(GameTime gameTime)
        {

            CGLOBAL.MousePrev = CGLOBAL.MouseCurrent;   //lưu trạng thái chuột vừa click
            CGLOBAL.MouseCurrent = Mouse.GetState();    // Lấy trạng thái chuột mới

            if (CGLOBAL.MouseCurrent.LeftButton == ButtonState.Pressed && CGLOBAL.MousePrev.LeftButton == ButtonState.Released)//double click
            {
                CGLOBAL.GUISelectedPrev = CGLOBAL.GUISelected;
                if (this._dstoado["btnExitPause"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.GUISelected = GUIenum.guiMainMenu;
                    CGLOBAL.NewGame = enumGame.mNew;
                    isSusses = false;
                }
                else if (this._dstoado["btnBackPause"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    //CGLOBAL.NextGUI = true;
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.GUISelected = GUIenum.guiRunningDeath;
                    isSusses = false;
                }
                else if (this._dstoado["btnSettingPause"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiSetting;
                    isSusses = false;
                }
                else if (this._dstoado["btnSaveGamePause"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))  // Contains = chứa cái gì
                {
                    isSusses = false;
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    initData();
                    string sdate =""+data.Date.Day + data.Date.Month + data.Date.Year+data.Date.Hour+data.Date.Minute +data.Date.Second +data.Date.Millisecond;
                    data.fileName = CGLOBAL.PlayerNameCurent + sdate+".sav";
                    SaveGame.doSave(CGLOBAL.PlayerNameCurent+sdate+".sav", data);
                    createRecGame();
                    isSusses = true;
                }
            }
        }

        private void createRecGame()
        {
            SaveGame.loadArrData();
            CGLOBAL.recGameData.Clear();

            Rectangle _recGameDataStand = new Rectangle(CThamSo.recDataStand.X, CThamSo.recDataStand.Y - 15, CThamSo.recDataStand.Width, CThamSo.recDataStand.Height);

            for (int i = 0; i < CGLOBAL.arrDataGame.Count; i++)
            {
                if (i == 0)
                {
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameData.Add(recdata);
                }
                else
                {
                    _recGameDataStand.Y = _recGameDataStand.Y + 33;
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameData.Add(recdata);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            this._spriteBatch.Begin();
            this._spriteBatch.Draw(this._texBG, Vector2.Zero,Color.Gray);
            this._spriteBatch.Draw(this._texBackground, new Vector2(this._dstoado["bgPause"].X, this._dstoado["bgPause"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnExitPause, new Vector2(this._dstoado["btnExitPause"].X, this._dstoado["btnExitPause"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnBack, new Vector2(this._dstoado["btnBackPause"].X, this._dstoado["btnBackPause"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnSetting, new Vector2(this._dstoado["btnSettingPause"].X, this._dstoado["btnSettingPause"].Y), Color.White);
            this._spriteBatch.Draw(this._textBtnSave, new Vector2(this._dstoado["btnSaveGamePause"].X, this._dstoado["btnSaveGamePause"].Y), Color.White);

            if(isSusses)
            {
                _spriteBatch.DrawString(fontChu, "Saved successfully !", new Vector2(this._dstoado["success"].X, this._dstoado["success"].Y), Color.White);
            }

            this._spriteBatch.End();
        }

        private void initData()
        {
            data.PlayerName = CGLOBAL.PlayerNameCurent;
            data.Life = CGLOBAL.Life;
            data.Score = CGLOBAL.Score;
            data.speedAt = CGLOBAL.speedDa;

            data.pointBeMat01 = CGLOBAL.pointBeMat01;
            data.pointBeMat02 = CGLOBAL.pointBeMat02;
            data.pointBeMat03 = CGLOBAL.pointBeMat03;
            data.recHo = CGLOBAL._recHo;
            data.pointDa = CGLOBAL.pointDa;
            data.Date = DateTime.Now;

            data.PointActor = CGLOBAL.pointActor;
           // data.dsChuoi = CGLOBAL.dsChuoi;
        }
    }
}
